<?php
/**
 * 《借款管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 832 2010-01-06 09:17:14Z xuy $
 */

require_once(dirname(__FILE__) . '/Expense.class.php');

class Borrow extends Expense {
	public $tbl = Constant::tbl_cw_borrow;

	public $type = array(
		'-' => '请选择',
		'1' => '储备金',
		'2' => '临时',		
		'3' => '私人',
		'4' => '预付货款',
		
		'9' => '其它',
	);
	
	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '等待领钱',
		'3' => '等待还钱',
		'4' => '已经还钱',

		'9' => '其它',
	);
	
	public $orderby = array(
		'-' => '请选择',
		'emp' => '借款人',
		'begin' => '借款日期',
		'end' => '还款日期',

		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('type_option', $this->type);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime("-3 month"));
		
		$emp = array(
			// '-' => '请选择',
			'emp' => '借款人',
			'oper' => '录入员',
		);
		
		$controller->assign_by_ref('emp_option', $emp);
		
		$controller->display($request, 'finance/borrow/find');
		return true;
	}
	
	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'mobile') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['mobile'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {		
		$name_s = $request['name_s'];
		$type_s = $request['type_s'];
		$payment_s = $request['payment_s'];
				
		$date1_s = $request['begin1_s'];
		$date2_s = $request['begin2_s'];
		
		$emp_t_s = $request['emp_t_s'];
		if ($emp_t_s == 'emp') {
			$request['emp_s'] = $request['emp_a_s'];
		} else if ($emp_t_s == 'oper') {
			$request['oper_s'] = $request['emp_a_s'];
		}

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
				
		if (!empty($name_s)) {
			$ss = '%' . $name_s . '%';
			$where .= sprintf(" AND (name LIKE '%s' OR content like '%s')", $ss, $ss);
		}
		
		if (!empty($type_s) && $type_s <> '-') {
			$where .= " AND type = '" . $type_s . "'";
		}
		
		if (!empty($payment_s) && $payment_s <> '-') {
			$where .= " AND payment = '" . $payment_s . "'";
		}
		
		if (!empty($date1_s)) {
			$where .= " AND begin >= '" . $date1_s . "'";
		}

		if (!empty($date2_s)) {
			$where .= " AND begin < '" . $date2_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}

		$emp = $_SESSION['id'];
		
		$p = array('1'=>'45', '2'=>'46', '3'=>'47', '9'=>'15');
		
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			$office = implode(",", $a);
			
			return sprintf(" visible = 1 AND (office in (%s) OR emp = '%s' OR oper = '%s')", $office, $emp, $emp);
		}
		
		$dept = $this->_where_dept($emp);
		if (!empty($dept)) {
			return sprintf(" visible = 1 AND (%s)", $dept);
		}

		return sprintf(" visible = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		} else {
			$controller->clear_assign('result');
		}

		$controller->display($request, 'finance/borrow/list');
		return true;
	}
	
	function xls($controller, $request) {
		$this->pretreat($controller);
		
		$this->findPara($controller, $request);

		$request['tpl'] = "xls";

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s LIMIT 50";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$row = $this->_orderby_x($controller, $row, $this->orderby_s);
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);

		$controller->display($request, 'finance/borrow/list');
		return true;
	}
	
	function txt($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "txt";
		
		$insql = $this->_batch_insql($request);
		if (empty($insql)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));

			$this->findPost($controller, $request);
			return false;
		}
		
		$my_bank = $this->_my_bank($_SESSION['id'], 2, '招商银行');
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			if ($row['payment'] != '2') {
				continue;
			}
			
			$row['bank'] = $this->_bank("1", $row['emp'], $row['payment_x']);
			if (!is_array($row['bank'])) {
				continue;
			}
			
			$row['transmit'] = $this->_transmit_type($row['bank'], $my_bank['zone'], $my_bank['city']);
			if (empty($row['transmit'])) {
				continue;
			}
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('bank', $my_bank);
		
		$content = $controller->sunxy($request, 'finance/borrow/list');
		$content = iconv("UTF-8", "GBK", $content);
		
		$filename = sprintf("BATCH-%s.PAY", date("YmdHis"));
		header('Content-Disposition:text/vcf; filename=' . $filename);
		
		$controller->assign_by_ref('content', $content);
		$controller->display($request, 'gbk');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('10 day'));

		$controller->display($request, 'finance/borrow/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$name = addslashes($request['name']);
		$type = $request['type'];
		$content = addslashes($request['content']);
			
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$begin = $request['begin'];
		$end = $request['end'];
		
		$amount = $request['amount'];
		$currency = $request['currency'];
		$payment = $request['payment'];
		$payment_x = addslashes($request['payment_x']);
		$this->_update_bank("1", $emp, $request['payment_x']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, name, type, content, begin, end, amount, currency, payment, payment_x, emp, oper, createtime, ";
		$fmt .= "modifytime) VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $name, $type, $content, $begin, $end, $amount, $currency, $payment, $payment_x, $emp, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/borrow/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);

		$name = addslashes($request['name']);
		$type = $request['type'];
		$content = addslashes($request['content']);
			
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$begin = $request['begin'];
		$end = $request['end'];
		
		$amount = $request['amount'];
		$currency = $request['currency'];
		$payment = $request['payment'];
		$payment_x = addslashes($request['payment_x']);
		$this->_update_bank("1", $emp, $request['payment_x']);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', name = '%s', type = '%s', content = '%s', emp = '%s', begin = '%s', end = '%s', ";
		$fmt .= "amount = '%s', currency = '%s', payment = '%s', payment_x = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $name, $type, $content, $emp, $begin, $end, $amount, $currency, $payment, $payment_x, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		
		$this->_files_clean($id);
		
		$row = $this->_view($id);
	
		$row['files'] = $this->_files($row['id']);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'finance/borrow/upload');
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/borrow/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->payment = $this->_shift($this->payment);
		
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$row['files'] = $this->_files($row['id']);
		
		$controller->assign_by_ref('result', $row);
		
		if (!empty($request['print'])) {
			$controller->display($request, 'finance/borrow/view_print');
		} else {
			$controller->display($request, 'finance/borrow/view');
		}
		
		return true;
	}

	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['type_t'] = $this->type[$row['type']];
		$row['payment_t'] = $this->payment[$row['payment']];
		$row['payment_x'] = json_decode($row['payment_x']);
		
		$row['t3'] = $this->_t3($row['end']);
		$row['day'] = $this->date_diff('d', $row['begin'], $row['end']);
		
		$row = $this->_view_t($row);

		return $row;
	}
	
	function _list($emp, $status = '3') {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND status = '%s' ORDER BY id";
		$sql = sprintf($fmt, $this->tbl, $emp, $status);
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
			
			$row['ok'] = true;			

			$ret[] = $row;
		}

		return $ret;
	}
	
	function verify($controller, $request) {
		$this->payment = $this->_shift($this->payment);
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/borrow/verify');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function verifyPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function finish($controller, $request) {
		$this->payment = $this->_shift($this->payment);
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		$email = array(
			'-' => '请选择',
			'1' => '是',		
			'2' => '否',
		);
		$controller->assign_by_ref('email_option', $email);

		$controller->display($request, 'finance/borrow/finish');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function finishPost($controller, $request) {
		$this->payment = $this->_shift($this->payment);
		
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
		
		$begin = $request['begin'];
		$email = $request['email'];
		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET content = concat(content, '\n%s'), begin = '%s', status = '3', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $begin, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
		
		$row = $this->_view($id);
		if ($email == '1' && $row['payment'] == '2') {			
			$from = $_SESSION['id'];
	
			$to = $row['emp'];
			// $to = "101";
			$row['to'] = Name::emp($to);
	
			$controller->assign_by_ref('result', $row);
			
			$subject = sprintf('借款 %s %s', $row['id'], $row['status_t']);
			$body = $controller->sunxy($request, 'finance/borrow/body_3');
			debug($body);
			
			$this->_notice($from, $to, $subject, $body, 1);
			if ($row['emp'] <> $row['oper']) {
				$this->_notice($from, $row['oper'], $subject, $body, 1);
			}
		}
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function returned($controller, $request) {
		$this->payment = $this->_shift($this->payment);
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		$email = array(
			'-' => '请选择',
			'1' => '是',		
			'2' => '否',
		);
		$controller->assign_by_ref('email_option', $email);

		$controller->display($request, 'finance/borrow/return');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function returnedPost($controller, $request) {
		$this->payment = $this->_shift($this->payment);
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
		
		$end = $request['end'];
		$email = $request['email'];
		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET content = concat(content, '\n%s'), end = '%s', status = '4', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $end, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
		
		$row = $this->_view($id);
		if ($email == '1' && $row['payment'] == '2') {			
			$from = $_SESSION['id'];
	
			$to = $row['emp'];
			// $to = "101";
			$row['to'] = Name::emp($to);
	
			$controller->assign_by_ref('result', $row);
			
			$subject = sprintf('借款 %s %s', $row['id'], $row['status_t']);
			$body = $controller->sunxy($request, 'finance/borrow/body_4');
			// debug($body);
			
			$this->_notice($from, $to, $subject, $body, 1);
			if ($row['emp'] <> $row['oper']) {
				$this->_notice($from, $row['oper'], $subject, $body, 1);
			}
		}
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function money($controller, $request) {
		$this->pretreat($controller);
		
		$id = $request['id'];
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);
		
		$controller->assign('t1', strtotime('-3 day'));

		$controller->display($request, 'finance/borrow/money');
		return true;
	}
	
	function moneyPost($controller, $request) {		
		require_once(dirname(__FILE__) . '/Money.class.php');
		$money = new Money();
		
		$money->addPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_code($controller, $request) {

		$data = $this->_ajax_code($request);
		echo json_encode($data);
	}

	function _ajax_code($request) {
		$q = $request['q'];
		$n = $request['maxRows'];
		if (empty($n)) {
			$n = 20;
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = '1' AND code LIKE '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl, $q . "%");
		// debug($sql);
		$result = $db->query($sql);

		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['emp_t'] = Name::emp($row['emp']);

			$ret[] = $row;
		}

		return $ret;
	}
	
	function n3($controller, $request) {
		$this->payment = $this->_shift($this->payment);
		
		$this->pretreat($controller);

		$from = $this->email;
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE type = '2' AND status = '3' AND end < now()";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
		
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
			
			$to = $row['emp'];
			// $to = "101";
	
			$controller->assign_by_ref('result', $row);
			
			$subject = sprintf('[系统通知]借款 %s', $row['id']);
			$body = $this->_tj_html($controller, $request, 'finance/borrow/notice_3');	
			
			$to = $this->_emp_email($row['emp']);
			// $to = "997264@qq.com";
			$this->mail_html($from, $to, $subject, $body);
			
			if ($row['emp'] <> $row['oper']) {
				$to = $this->_emp_email($row['oper']);
				// $to = "997264@qq.com";
				$this->mail_html($from, $to, $subject, $body);
			}
		}

		logdate($controller, $request);
		return true;
	}
	
	function tj1($controller, $request) {
		$a = array(
			'南山借款'=>array('a'=>'45', 'office'=>'1'),
			'沙井借款'=>array('a'=>'46', 'office'=>'2'),
			'清溪借款'=>array('a'=>'47', 'office'=>'3'),
		);
		
		while (list($k, $v) = each($a)) {
			$request['title'] = $k;
			$request['a'] = $v['a'];
			$request['office'] = $v['office'];
						
			$this->tj($controller, $request);
		}
	}
	
	function tj2($controller, $request) {
		$a = array(
			'南山借款（周报）'=>array('a'=>'45', 'office'=>'1'),
			'沙井借款（周报）'=>array('a'=>'46', 'office'=>'2'),
			'清溪借款（周报）'=>array('a'=>'47', 'office'=>'3'),
		);
		
		while (list($k, $v) = each($a)) {
			$request['title'] = $k;
			$request['a'] = $v['a'];
			$request['office'] = $v['office'];
			
			$this->tjw($controller, $request);
		}				
	}
	
	function tj($controller, $request) {		
		$bb = $this->_list_email($request['a']);
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = $request['title'];		
		$office = $request['office'];
				
		$d2 = $this->_tj_df("createtime", "%Y-%m-%d");
		$d3 = $this->_tj_df("modifytime", "%Y-%m-%d");
		
		$dd = $this->_tj_df_not("%Y-%m-%d");
		
		$cc = array(
			'到期借款'=>array('w' => "office = '$office' AND type = '2' AND status = '3' AND end < now()", 'b'=>'id DESC'),	
			'新增信息'=>array('w' => "office = '$office' AND $d2", 'b'=>'id DESC'),
			'更新信息'=>array('w' => "office = '$office' AND $d3 AND $dd", 'b'=>'id DESC'),			
		);
		
		if ($this->_tj_cc($controller, $cc)) {
			echo "222";
			return;
		}

		$controller->clear_assign('result');
		
		$body = $this->_tj_html($controller, $request, 'finance/borrow/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
		
	function tjw($controller, $request) {		
		$bb = $this->_list_email($request['a']);
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$this->pretreat($controller);

		$from = $this->email;
		$subject = $request['title'];		
		$office = $request['office'];
		
		$d1 = $this->_tj_df("end", "%Y-%V");
		$d2 = $this->_tj_df("createtime", "%Y-%V");
		$d3 = $this->_tj_df("modifytime", "%Y-%V");
		
		$dd = $this->_tj_df_not("%Y-%m-%d");
		
		$cc = array(
			'已经还款'=>array('w' => "office = '$office' AND $d1 AND status = '4'", 'b'=>'id DESC'),	
			'新增信息'=>array('w' => "office = '$office' AND $d2", 'b'=>'id DESC'),
			'更新信息'=>array('w' => "office = '$office' AND $d3 AND $dd", 'b'=>'id DESC'),			
		);
		
		$this->_tj_cc($controller, $cc);
		
		$aa = array(
			'一个星期之内'=>array('w' => "WHERE office = '$office' AND createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -7 DAY), '%Y-%m-%d')"),
			'一个月之内'=>array('w' => "WHERE office = '$office' AND createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 MONTH), '%Y-%m-%d')"),
			'三个月之内'=>array('w' => "WHERE office = '$office' AND createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -3 MONTH), '%Y-%m-%d')"),
			'半年之内'=>array('w' => "WHERE office = '$office' AND createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -6 MONTH), '%Y-%m-%d')"),
			'一年之内'=>array('w' => "WHERE office = '$office' AND createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 YEAR), '%Y-%m-%d')"),
			'全部'=>array('w' => "WHERE office = '$office'")
		);
		
		$this->_tj_bb($controller, $aa);
			
		$body = $this->_tj_html($controller, $request, 'finance/borrow/tj_list');
				
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
		
	function _tj2($v) {
		$this->result_s = 10;
		$this->groupby_s = 'currency';
		$s = $this->groupby_s;

		if (!empty($v['w'])) {
			$fmt = "SELECT %s AS a, SUM(amount) AS c FROM %s a %s GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $v['w'], $s);
		} else {
			$fmt = "SELECT %s AS a, SUM(amount) AS c FROM %s a GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $s);
		}
		echo $sql . "\n";

		return $this->_group_tj2($sql);
	}
	
	function z($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE payment = '2' AND payment_x is NULL";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$payment_x = $this->_payment_x($row['emp']);			
			
			$fmt = "UPDATE %s SET payment_x = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $payment_x, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
	
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$row = $this->_view($id);
			if (!$this->_is_delete($row)) {
				continue;
			}

			$this->_delete3($id);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _delete3($id) {
		$db = newdb($this->dsn_w);
		if ($this->_is_admin()) {
			$fmt = "DELETE FROM %s WHERE id = '%s'";
		} else {
			$fmt = "UPDATE %s SET visible = 2 WHERE id = '%s'";
		}
		
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$db->query($sql);
	}
	
	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['status'] > 2) {
			return false;
		}

		if ($this->_audit('149') || $this->_audit('150')) {
			return true;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}
	
	function _return($s) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET end = now(), status = '4', modifytime = now()  WHERE id = '%s'";
		
		$bb = explode(',', $s);
		while (list($no, $id) = each($bb)) {
			$sql = sprintf($fmt, $this->tbl, $id);
			debug($sql);
			$db->query($sql);
		}
	}
}
?>